<?php
require_once 'includes/image_resizer.php'; 
$smsg="";
$store = "";

$storeid = $_GET['updstore'];

if(!empty($_REQUEST['hidAddStore']))
{
	$today=date("Y-m-d G:i:s",time());	
	$store_name=$_REQUEST['store_name'];	
	$store_location=$_REQUEST['store_location'];	
	$store_description=$_REQUEST['store_description'];	
	$store_guidelines=$_REQUEST['store_guidelines'];
	$newsletter_status=$_REQUEST['newsletter_status'];	
	$offerStatus=$_REQUEST['offer_status'];	
	if(isset($offerStatus)) {
		$offer = $_REQUEST['store_offer'];
	}
	else {
		$offer = ""; 
	}	
	$sqlAddStore="UPDATE tbl_seller_store set seller_name='$userName', store_name='$store_name',							
				store_description='$store_description',
				store_location='$store_location',				
				store_guidelines='$store_guidelines',
				offer_settings='$offerStatus',
				offer='$offer',
				newsletter_settings='$newsletter_status' 
				WHERE seller_store_id='$storeid'";
				
	$rstAddStore=mysql_query($sqlAddStore) or die(mysql_error());
		
	if(mysql_affected_rows()>0) {	
		$smsg="true";		
		
		//select previous store images
		$sqlSelStoreImage = "SELECT store_image, store_image_thumb FROM tbl_seller_store WHERE seller_store_id='$storeid'";
		$rstSelStoreImage = mysql_query($sqlSelStoreImage) or die(mysql_error());
		$rowSelStoreImage = mysql_fetch_array($rstSelStoreImage);
		
		
		$storeImage=pathinfo($_FILES["store_image"]["name"]);		
		$type = strtolower($storeImage['extension']);
		
			if(!empty($type)) {
						
				if($type=="jpeg" || $type=="jpg" || $type=="png" || $type=="gif") {											
					$orgname=mt_rand();												
					$path="store/source/".$orgname.$storeid.".".$type;					
					$path_thumb="store/thumb/".$orgname.$storeid."_thumb.".$type;					
					$path_medium="store/medium/".$orgname.$storeid."_medium.".$type;									
					move_uploaded_file($_FILES["store_image"]["tmp_name"],$path);				  
					productResize($path, $path_thumb, $path_medium, $type);	
					
					$sql_updpro="UPDATE tbl_seller_store SET 
								 store_image='".$path_medium."',
								 store_image_thumb='".$path_thumb."'								 
								 where seller_store_id=$storeid";	
					$rst_updpro=mysql_query($sql_updpro) or die(mysql_error());
					if(mysql_affected_rows()==0) { 
					
						$smsg="false";
						if($rowSelStoreImage[0]!="") { unlink($rowSelStoreImage[0]); } 
						if($rowSelStoreImage[1]!="") { unlink($rowSelStoreImage[1]); }
					
					}
					unlink($path);	
				}
			 }		
	}
	else { $smsg="false"; }	
}


//select all store info
 $sqlSelStore="SELECT * FROM tbl_seller_store WHERE seller_id='$userId' AND seller_store_id='$storeid'";
 $rstSelStore=mysql_query($sqlSelStore) or die(mysql_error());
 if(mysql_num_rows($rstSelStore)==0) {
 	echo "<script type='text/javascript'> window.location='index.php'; </script>";
	exit;
 }
 
 $rowSelStore=mysql_fetch_array($rstSelStore);
	 
?>
<script type="text/javascript">
	function funAddStore() {
		var x = document.getElementById("frmAddStore");
		document.getElementById("hidAddStore").value="Add store";
		if(valAddStore()) {
			x.submit();
		}
	}
	
	function valAddStore() {
		var store = document.getElementById("store_name");
		var desc = document.getElementById("store_description");
		if(store.value=="") {
			alert("Please enter Store name");
			store.focus();
			return false;
		}
		if(desc.value=="") {
			alert("Please enter store description");
			desc.focus();
			return false;
		}
		return true;					
	}
	
function funcheckOffer() {
	var offerStatus = document.getElementById("offer_status");
	if(offerStatus.checked==true) {
		document.getElementById("store_offer_div").value="";
		document.getElementById("store_offer_div").style.display="block";
	} else {		
		document.getElementById("store_offer_div").style.display="none";		
	}
}
</script>
<style type="text/css">
	#sell-form { 
		border:0px solid #CCC; vertical-align:top; padding:10px; float:left; 
		width:93%; margin:2%; 
	}

	#sell-form label{
	float: left;
	width: 130px;
	font-weight: bold;
	}
	
	#sell-form input, select, textarea{	
	width: 280px;
	margin-bottom: 5px;
	font-size:12px;
	}
	
	#sell-form textarea{
	height: 250px;
	width: 450px;
	font-size:12px
	}
	
	#sell-form #submitbutton{
	margin-left: 130px;
	margin-top: 5px;
	width: 90px;
	}
	
	#sell-form br{
	clear: left;
	}

	
</style>
<div id='panel'>
<table width="100%" border="0">
  <tr>
    <td class="contentHeader"><h2>Seller Store</h2></td>
  </tr>
  <tr>
    <td>
	<?php
	if($smsg=="true") { 
		echo "<script type='text/javascript'> window.location='seller_store.php?sid=$userId'; </script>";
	} 
	
	?>			
				<table width="100%" align="center"  border="0" cellspacing="0">
				  <tr>
					<td>&nbsp;</td>
				  </tr>
				  <tr>
					<td height="500">		
					<form name="frmAddStore" id="frmAddStore" method="post" action="" enctype="multipart/form-data">    
						  <input type="hidden" name="hidAddStore" id="hidAddStore">
							<div id="sell-form">														
								
								<label for="store_name">Store Name</label>
								<input type="text" name="store_name" id="store_name" style="width:450px;" value="<?php echo $rowSelStore['store_name']; ?>"><br>
								
								<label for="store_description">Description</label>
								<textarea name="store_description" id="store_description"><?php echo $rowSelStore['store_description']; ?></textarea><br>					
								
								<label for="store_location">Location</label>
								<input type="text" name="store_location" id="store_location" value="<?php echo $rowSelStore['store_location']; ?>">
								<br>			
								
								<label for="store_image">Store Image</label>
								<input type="file" name="store_image" id="store_image"><?php if($rowSelStore['store_image']!="") { echo "<a href='".$rowSelStore['store_image']."'>View</a>"; } ?><br>
																																					
								<label for="store_guidelines">Guidelines</label>
								<textarea name="store_guidelines" id="store_guidelines" style="height:60px; font-size:12px;"><?php echo $rowSelStore['store_guidelines']; ?></textarea><br>
								
								<label for="offer_status">Offer</label>										
								<input type="checkbox" value="1" style="width:auto;" name="offer_status" id="offer_status" onClick="funcheckOffer();" <?php if($rowSelStore['offer_settings']==1) echo "checked"; ?>>
								Yes<br>
								
								<span style="display:<?php if($rowSelStore['offer_settings']==1) echo "display"; ?>;" id="store_offer_div">
									<label for="store_offer">Offer details</label>		
									<textarea name="store_offer" id="store_offer" style="height:60px; font-size:12px;"><?php echo $rowSelStore['offer'] ?></textarea><br>
								</span>
								
								<label for="newsletter_status">Newsletter</label>										
								<input type="checkbox" style="width:auto;" name="newsletter_status" id="newsletter_status" value="1" <?php if($rowSelStore['newsletter_settings']==1) echo "checked"; ?>>
								Yes <br>
								
								<input type="button" onClick="funAddStore();" id="submitbutton" name="submitbutton" value="Submit"><br>
							</div><br>
						
					</form></td>
				  </tr>
				</table>
	
	</td>
  </tr>
</table>
</div>